--
-- Author:      feilong
-- DateTime:    2020-02-27 10:59:15
-- Description: 1945游戏场景


local Player = require("1945/Player")
local Enemy = require("1945/Enemy")
local bump = require("lib/bump")
local ResLoader = require("1945.ResLoader")


local Scene = require("ui.Scene")
local Game = class("Game", Scene)

function Game:ctor()
	self.super.ctor(self)

	local res_loader = ResLoader.new() --资源列表
	global.res_loader = res_loader
	
	self.world = bump.newWorld(64) --碰撞世界
	self.bullets = {} --子弹列表
	self.planes = {} --飞机列表
	self.others = {} --
	self.timer = 0
	self.count = 0
	self.planes[1] = Player.new(self, 100,500, 0) --玩家飞机
	for i = 1, 5 do
		table.insert(self.planes, Enemy.new(self))
	end
	
	self.map_list = {
		global.res_loader.bg01,
		global.res_loader.bg02,
		global.res_loader.bg03,
	}
	self.scene_h = 768
	self.map_y = self.scene_h

end

function Game:exit()

end

function Game:update(dt)
	self.timer = self.timer + dt
	for i = #self.bullets,1 ,-1 do
		local go = self.bullets[i]
		go:update(dt)
		if go.destroyed then 
			self.world:remove(go)
			table.remove(self.bullets,i) 
		end
	end
	for i = #self.planes,1 ,-1 do
		local go = self.planes[i]
		go:update(dt)
		if go.destroyed then
			self.world:remove(go)
			table.remove(self.planes,i) 
		end
	end
	for i = #self.others,1 ,-1 do
		local go = self.others[i]
		go:update(dt)
		if go.destroyed then
			table.remove(self.others,i) 
		end
	end
	if #self.planes<6 then --保持有6个飞机在游戏中
		table.insert(self.planes,Enemy.new(self))
	end
end

function Game:draw()
	self.map_y = self.map_y + 1
	if self.map_y > self.scene_h*2 then 
		self.map_y = self.scene_h
	end
	for k, v in pairs(self.map_list) do 
		love.graphics.draw(v, 0, self.map_y - self.scene_h*k)
	end

	for i,v in ipairs(self.bullets) do
		v:draw()
	end
	for i,v in ipairs(self.planes) do
		v:draw()
	end
	for i,v in ipairs(self.others) do
		v:draw()
	end


end


return Game